package com.java110.mall.cart.cmd.cart;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.storeOrderEngine.StoreGroupOrderEngine;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 拼团下单接口
 */
@Java110Cmd(serviceCode = "cart.unifiedGroupProductOrder")
public class UnifiedGroupProductOrderCmd extends Cmd {


    @Autowired
    private StoreGroupOrderEngine storeGroupOrderEngine;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        Assert.hasKeyAndValue(reqJson, "shopId", "未包含商铺信息");
        Assert.hasKeyAndValue(reqJson, "groupId", "未包含拼团ID");
        Assert.hasKeyAndValue(reqJson, "valueId", "未包含规格ID");
        Assert.hasKeyAndValue(reqJson, "productId", "未包含商品");
        Assert.hasKeyAndValue(reqJson, "orderId", "未包含订单ID");
        Assert.hasKeyAndValue(reqJson, "addressId", "未包含地址");
        Assert.hasKeyAndValue(reqJson, "goodsNum", "未包含购买数量");
        Assert.hasKeyAndValue(reqJson, "payAmount", "未包含支付金额");

    }

    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        String userId = cmdDataFlowContext.getReqHeaders().get("user-id");
        String appId = cmdDataFlowContext.getReqHeaders().get("app-id");

        reqJson.put("user-id", userId);//
        reqJson.put("app-id", appId);//
        storeGroupOrderEngine.unifiedOrder(reqJson);

    }


}
